package org.wk.core.common.unitofwork;

/**
 * 
 * @ClassName	: Processor
 * @Package		：org.wk.core.web.unitofwork
 * @Author df.w 
 * @Create Time	：2018年5月2日 上午10:10:36
 * @Description	: Either 类的 left 包含异常（如有），而 right 包含结果。
 */
public abstract class Processor {
	public Either<? extends Exception,Boolean> execute(){
		try{
			process();
			return Either.right(Boolean.TRUE);
		}catch(Exception e){
			return Either.left(e);
		}
	}
	public Either<? extends Exception,Boolean> executeProcessException(Exception e){
		try{
			processException(e);
			return Either.right(Boolean.TRUE);
		}catch(Exception exception){
			return Either.left(exception);
		}
	}
	public abstract void process();
	public void processException(Exception e){}
	public static class Either<A,B>{
		 private A left = null;
		 private B right = null;
		 private Either(A a,B b){
			 this.left = a;
			 this.right = b;
		 }
		 public static <A,B> Either<A,B> left(A a) {
		        return new Either<A,B>(a,null);
		 }
		 public A left() {
		        return left;
		    }
	     public boolean isLeft() {
	        return left != null;
	     }

	     public boolean isRight() {
	        return right != null;
	     }

	     public B right() {
	        return right;
	     }
	     public static <A,B> Either<A,B> right(B b) {
	        return new Either<A,B>(null,b);
	     }
	}

}
